package ks.client.controllers.requests;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ks.client.UserContext;
import ks.client.gui.GMGUI;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;
import ks.framework.interfaces.IProcessMessage;
import ks.managers.table.Table;
import ks.managers.table.TableManager;

/**
 * The controller for the table-info request.
 * 
 * @author Jhunt
 */
public class TableInfoController implements IProcessMessage {

	/** In case we need to process something. */
	Lobby lobby;
	
	/** Construct controller with lobby. */
	public TableInfoController(Lobby lobby) {
		this.lobby = lobby;
	}

	/**
	 * Client has received table-info message. Process it.
     * 
	 * @param m The message to process.
	 * @return true on success; false on failure.
	 */
	public boolean process(Message m) {
		// Get context
		UserContext context = lobby.getUserContext();
		
		// extract information from the message
		Node tableInfoNode = m.contents();
		
		if (tableInfoNode == null) {
			return false;
		}
		
		TableManager tm = context.getTableManager();
		
		NodeList tablesList = tableInfoNode.getChildNodes();
		Node tableNode = null;
		NamedNodeMap tableAttributes;
		Node idNode, openNode, timeNode, pluginNode, undoNode, newhandNode, moderatorNode;
		NodeList playernamesList;
		Node playernameNode;
		Node nameNode;
		
		for (int i = 0; i < tablesList.getLength(); i++) {
			tableNode = tablesList.item(i);
			
			if (tableNode == null) {
				continue;
			}
			
			tableAttributes = tableNode.getAttributes();
			
			idNode = tableAttributes.getNamedItem("id");
			openNode = tableAttributes.getNamedItem("open");
			timeNode = tableAttributes.getNamedItem("time");
			pluginNode = tableAttributes.getNamedItem("plugin");
			undoNode = tableAttributes.getNamedItem("undo");
			newhandNode = tableAttributes.getNamedItem("newhand");
			moderatorNode = tableAttributes.getNamedItem("moderator");
			
			if (idNode == null) {
				continue;
			}
			
			Table table = tm.getTable(Integer.parseInt(idNode.getNodeValue()));
			
			if (openNode != null) {
				//table.setOpen(openNode.getNodeValue());
			}
			if (timeNode != null) {
				//table.setTime(timeNode.getNodeValue());
			}
			if (pluginNode != null) {
				//table.setPlugin(pluginNode.getNodeValue());
			}
			if (undoNode != null) {
				//table.setUndo(Boolean.parseBoolean(undoNode.getNodeValue()));
			}
			if (newhandNode != null) {
				//table.setNewand(Boolean.parseBoolean(newhandNode.getNodeValue()));
			}
			if (moderatorNode != null) {
				table.setModerator(moderatorNode.getNodeValue());
			}
			
			playernamesList = tableNode.getChildNodes();
			
			table.resetTable();
			
			for (int j=0; j<playernamesList.getLength(); j++) {
				playernameNode = playernamesList.item(j);
				
				if (playernameNode == null) {
					continue;
				}
				
				nameNode = playernameNode.getAttributes().getNamedItem("name");
				
				if (nameNode == null) {
					continue;
				}
				
				table.addPlayer(nameNode.getNodeValue());
			}
		}
		
		GMGUI gui = lobby.getGMGUI();
		if (gui != null) {
			lobby.getGMGUI().updateUserList();
		}
		
		return true;
	}
}
